package com.zte.sys.user.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zte.sys.dept.model.Dept;
import com.zte.sys.position.model.Job;
import com.zte.sys.user.dao.IUserDao;
import com.zte.sys.user.model.Users;
import com.zte.sys.user.model.UsersCondition;
import com.zte.sys.user.service.IUserService;
import com.zte.util.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class UserService implements IUserService {

    @Autowired
    private IUserDao userDao;

    /**
     * 分页查询
     * @param conn
     * @param pageModel
     * @return
     */
    @Override
    public PageModel<Users> queryUserPageByCondition(UsersCondition conn, PageModel<Users> pageModel) {
        if (conn.getUIdlike().equals("null")){
            conn.setUIdlike("");
        }
        if (conn.getUname().equals("null")){
            conn.setUname("");
        }
        if(conn.getDeptNamelike().equals("null")){
            conn.setDeptNamelike("");
        }
        if (conn.getStatusId().equals("null") ){
            conn.setStatusId("");
        }
        int pageNum = pageModel.getPageNum();
        pageNum = pageNum<=0?1:pageNum;
        int pageSize = 3;
        PageHelper.startPage(pageNum,pageSize);
        List<Users> users = userDao.queryUserListByCondition(conn);
        PageInfo<Users> info = new PageInfo<Users>(users);

        pageModel.setPageNum(info.getPageNum());
        pageModel.setPageSize(info.getPageSize());
        pageModel.setTotalPage(info.getPages());
        pageModel.setTotalCount((int)info.getTotal());
        pageModel.setData(new ArrayList<Users>(info.getList()));
        return pageModel;
    }

    /**
     * 查部门
     * @return
     */
    @Override
    public List<Dept> queryDeptList(String deptId) {
        List<Dept> depts = userDao.queryDeptList(deptId);
        return depts;
    }

    /**
     * 查职位
     * @return
     */
    @Override
    public List<Job> queryJobList(String jobId) {
        List<Job> jobs = userDao.queryJobList(jobId);
        return jobs;
    }

    /**
     * 详情
     * @param conn
     * @return
     */
    @Override
    public Users queryUserByCondition(UsersCondition conn){
        return userDao.queryUserByCondition(conn);
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @Override
    public int addUser(Users user) {
        user.setStatusId("1");
        user.setHiredate(new Date());
        return userDao.addUser(user);
    }

    /**
     * 修改用户
     * @param users
     * @return
     */
    @Override
    public int updateUser(Users users) {
        if(users.getStatusId().equals("1")){
            users.setHiredate(new Date());
        }
        return userDao.updateUser(users);
    }

    /**
     * 入职撤职
     * @param users
     * @return
     */
    @Override
    public int updateStatusId(Users users) {
        if(users.getStatusId().equals("1")){
            users.setLeavedate(new Date());
            users.setStatusId("2");
        }else {
            users.setStatusId("1");
            users.setHiredate(new Date());
        }
        return userDao.updateStatusId(users);
    }

}
